{#
Copyright 2025 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

#}{% extends 'base.html' %}
{% import 'macros.html' as macros %}

{% block content %}

{% if index_css_content %}
<style>
{{ index_css_content | safe }}
</style>
{% endif %}

{% if index_js_content %}
<script>
{{ index_js_content | safe }}
</script>
{% endif %}

<div x-data="{
    benchmarkOpen: true,
    projectOpen: true,
    languageOpen: true,
    crashesFoundOpen: true,
    ofgMetricsOpen: true,
    cyclesOpen: true,
    chartsOpen: true,
}" class="space-y-2">

    <div class="border rounded-lg toc-section">
        <button @click="benchmarkOpen = !benchmarkOpen"
                class="w-full p-4 flex justify-between items-center">
            <span class="text-lg font-bold">
                Benchmark summary
            </span>
            {{ macros.icon_chevron_down('w-5 h-5', 'benchmarkOpen') }}
        </button>

        <div x-show="benchmarkOpen"
            class="p-4 border-t">
            <div class="overflow-x-auto">
                <table class="sortable-table min-w-full" id="benchmark-summary-table">
                    <thead>
                        <tr>
                            <th></th>
                            <th data-sorted="asc">Benchmark</th>
                            <th>Status</th>
                            <th data-sort-number>Build Rate</th>
                            <th data-sort-number>Crash Rate</th>
                            <th data-sort-number>Bugs</th>
                            <th data-sort-number>Program Counter Coverage</th>
                            <th data-sort-number>Line Coverage Diff</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for benchmark in benchmarks %}
                        <tr>
                            <td class="table-index">{{ loop.index }}</td>
                            <td data-sort-value="{{ benchmark.id }}">
                                <div class="project">{{ benchmark.project }}</div>
                                <pre class="signature">
                                    <a class="prettify-benchmark-name" href="benchmark/{{ benchmark.id|urlencode }}/index.html">{{ benchmark.signature }}</a>
                                </pre>
                            </td>
                            <td data-sort-value="{{ benchmark.status }}">{{ benchmark.status }}</td>
                            <td data-sort-value="{{ benchmark.result.build_success_rate|percent }}">{{ benchmark.result.build_success_rate|percent}}</td>
                            <td data-sort-value="{{ benchmark.result.crash_rate|percent }}"><a href="benchmark/{{ benchmark.id|urlencode }}/crash.json"> {{ benchmark.result.crash_rate|percent }} </a></td>
                            <td data-sort-value="{{ benchmark.result.found_bug }}">{{ benchmark.result.found_bug }}</td>
                            <td data-sort-value="{{ benchmark.result.max_coverage |percent }}">{{ benchmark.result.max_coverage |percent }}</td>
                            <td data-sort-value="{{ benchmark.result.max_line_coverage_diff|percent }}"><a href="{{ benchmark.result.max_coverage_diff_report | cov_report_link }}">{{ benchmark.result.max_line_coverage_diff|percent }}</a></td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>

            </div>
        </div>

    </div>

    <div class="border rounded-lg toc-section">
        <button @click="projectOpen = !projectOpen"
                class="w-full p-4 flex justify-between items-center">
            <span class="text-lg font-bold">
                Project summary
            </span>
            {{ macros.icon_chevron_down('w-5 h-5', 'projectOpen') }}
        </button>
        <div x-show="projectOpen"
             class="p-4 border-t">

            {{ macros.expand_collapse_buttons('project-summary-expand-all', 'project-summary-collapse-all') }}

            <div class="overflow-x-auto">
                <table class="sortable-table min-w-full" id="project-summary-table">
                    <thead>
                        <tr>
                            <th></th>
                            <th data-sorted="asc">Project</th>
                            <th data-sort-number>Total benchmarks</th>
                            <th data-sort-number>Successful benchmarks</th>
                            <th data-sort-number>Total coverage gain</th>
                            <th data-sort-number>Total relative gain</th>
                            <th data-sort-number>OSS-Fuzz-Gen total covered lines</th>
                            <th data-sort-number>OSS-Fuzz-Gen new covered lines</th>
                            <th data-sort-number>Existing covered lines</th>
                            <th data-sort-number>Total project lines</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- TODO: Revisit coverage and relative gains in the future, currently they're sums of percentages -->
                        {% set ns_projects = namespace(
                            sum_successful_benchmarks=0,
                            sum_total_coverage_gain=0,
                            sum_total_relative_gain=0,
                            sum_ofg_total_covered_lines=0,
                            sum_ofg_new_covered_lines=0,
                            sum_existing_covered_lines=0,
                            sum_total_project_lines=0) %}

                        {% for project in projects %}

                        {% set ns_projects.sum_successful_benchmarks = ns_projects.sum_successful_benchmarks + project.count %}
                        {% set ns_projects.sum_ofg_total_covered_lines = ns_projects.sum_ofg_total_covered_lines + project.coverage_ofg_total_covered_lines %}
                        {% set ns_projects.sum_ofg_new_covered_lines = ns_projects.sum_ofg_new_covered_lines + project.coverage_ofg_total_new_covered_lines %}
                        {% set ns_projects.sum_existing_covered_lines = ns_projects.sum_existing_covered_lines + project.coverage_existing_total_covered_lines %}
                        {% set ns_projects.sum_total_project_lines = ns_projects.sum_total_project_lines + project.coverage_existing_total_lines %}
                        <tr class="project-data-row">
                            <td class="table-index">
                                <button class="w-5 h-5 flex items-center justify-center focus:outline-none" @click="$refs.benchmarks_{{ loop.index }}.classList.toggle('hidden')">
                                    {{ macros.icon_chevron_down('w-4 h-4', '!$refs.benchmarks_' ~ loop.index ~ '.classList.contains(\'hidden\')') }}
                                </button>
                            </td>
                            <td data-sort-value="{{ project.name }}" class="w-full">
                                <button @click="$refs.benchmarks_{{ loop.index }}.classList.toggle('hidden')" class="text-left hover:text-blue-600 focus:outline-none toc-subsection">
                                    {{ project.name }}
                                    {% set ns = namespace(
                                        bug_found=false,
                                        aggregated_build_rate=0,
                                        aggregated_crash_rate=0,
                                        aggregated_found_bug=0,
                                        aggregated_max_coverage=0,
                                        aggregated_max_line_coverage_diff=0) %}
                                    {% for benchmark in benchmarks %}
                                        {% if benchmark.project == project.name %}
                                            {% if benchmark.result.found_bug %}
                                                {% set ns.bug_found = true %}
                                                <!-- TODO: Track the average number of bugs as well, in order to identify if a single benchmark has an exceptionally high bug count making it an outlier that needs priority fixing -->
                                            {% endif %}
                                            {% set ns.aggregated_build_rate = ns.aggregated_build_rate + benchmark.result.build_success_rate %}
                                            {% set ns.aggregated_crash_rate = ns.aggregated_crash_rate + benchmark.result.crash_rate %}
                                            {% set ns.aggregated_found_bug = ns.aggregated_found_bug + benchmark.result.found_bug %}
                                            {% set ns.aggregated_max_coverage = ns.aggregated_max_coverage + benchmark.result.max_coverage %}
                                            {% set ns.aggregated_max_line_coverage_diff = ns.aggregated_max_line_coverage_diff + benchmark.result.max_line_coverage_diff %}
                                        {% endif %}
                                    {% endfor %}
                                    <span class="status-indicator ml-1 {{ 'has_bugs' if ns.bug_found else 'no_bugs' }}"></span>
                                </button>
                            </td>
                            <td data-sort-value="{{ project.count }}">{{ project.count }}</td>
                            <td data-sort-value="{{ project.success }}">{{ project.success }}</td>
                            <td data-sort-value="{{ project.coverage_gain|percent }}">{{ project.coverage_gain|percent }}%</td>
                            <td data-sort-value="{{ project.coverage_relative_gain|percent}}">{{ project.coverage_relative_gain | percent }}%</td>
                            <td data-sort-value="{{ project.coverage_ofg_total_covered_lines}}">{{project.coverage_ofg_total_covered_lines }}</td>
                            <td data-sort-value="{{ project.coverage_ofg_total_new_covered_lines}}">{{ project.coverage_ofg_total_new_covered_lines }}</td>
                            <td data-sort-value="{{ project.coverage_existing_total_covered_lines}}">{{ project.coverage_existing_total_covered_lines }}</td>
                            <td data-sort-value="{{ project.coverage_existing_total_lines}}">{{ project.coverage_existing_total_lines }}</td>
                        </tr>
                        <tr class="project-benchmarks-container-row">
                            <td colspan="10" class="">
                                <div x-ref="benchmarks_{{ loop.index }}" class="hidden">
                                    <div class="overflow-x-auto">
                                        <table class="sortable-table min-w-full ml-8" id="benchmarks-table-{{ loop.index }}">
                                        <thead>
                                            <tr>
                                                <th></th>
                                                <th data-sorted="asc" data-sort-string>Benchmark</th>
                                                <th data-sort-string>Status</th>
                                                <th data-sort-number>Build Rate</th>
                                                <th data-sort-number>Crash Rate</th>
                                                <th data-sort-number>Bugs</th>
                                                <th data-sort-number>Program Counter Coverage</th>
                                                <th data-sort-number>Line Coverage Diff</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {% set bench_ns = namespace(index=0) %}
                                            {% for benchmark in benchmarks %}
                                            {% if benchmark.project == project.name %}
                                            {% set bench_ns.index = bench_ns.index + 1 %}
                                            <tr>
                                                <td class="table-index">
                                                    {{ bench_ns.index }}
                                                </td>
                                                <td data-sort-value="{{ benchmark.id }}">
                                                    <pre class="signature toc-item">
                                                        <a href="benchmark/{{ benchmark.id|urlencode }}/index.html" class="prettify-benchmark-name">{{ benchmark.signature }}</a>
                                                    </pre>
                                                </td>
                                                <td data-sort-value="{{ benchmark.status }}">{{ benchmark.status }}</td>
                                                <td data-sort-value="{{ benchmark.result.build_success_rate|percent }}">{{ benchmark.result.build_success_rate|percent}}</td>
                                                <td data-sort-value="{{ benchmark.result.crash_rate|percent }}"><a href="benchmark/{{ benchmark.id|urlencode }}/crash.json"> {{ benchmark.result.crash_rate|percent }} </a></td>
                                                <td data-sort-value="{{ benchmark.result.found_bug if benchmark.result and benchmark.result.found_bug is not none else 'N/A' }}">{{ benchmark.result.found_bug if benchmark.result and benchmark.result.found_bug is not none else 'N/A' }}</td>
                                                <td data-sort-value="{{ benchmark.result.max_coverage |percent }}">{{ benchmark.result.max_coverage |percent }}</td>
                                                <td data-sort-value="{{ benchmark.result.max_line_coverage_diff|percent }}"><a href="{{ benchmark.result.max_coverage_diff_report | cov_report_link }}">{{ benchmark.result.max_line_coverage_diff|percent }}</a></td>
                                            </tr>
                                            {% endif %}
                                            {% endfor %}
                                            <tr class="font-bold">
                                                <td></td>
                                                <td>Average</td>
                                                <td>-</td>
                                                <td>{{ (ns.aggregated_build_rate / project.count)|percent }}</td>
                                                <td>{{ (ns.aggregated_crash_rate / project.count)|percent }}</td>
                                                <td>{{ ns.aggregated_found_bug }}</td>
                                                <td>{{ (ns.aggregated_max_coverage / project.count)|percent }}</td>
                                                <td>{{ (ns.aggregated_max_line_coverage_diff / project.count)|percent }}</td>
                                            </tr>
                                        </tbody>
                                    </table>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                        {% if ns_projects.sum_total_project_lines > 0 %}
                            {% set ns_projects.sum_total_coverage_gain = ns_projects.sum_ofg_new_covered_lines / ns_projects.sum_total_project_lines %}
                        {% else %}
                            {% set ns_projects.sum_total_coverage_gain = 0 %}
                        {% endif %}

                        {% if ns_projects.sum_existing_covered_lines > 0 %}
                            {% set ns_projects.sum_total_relative_gain = ns_projects.sum_ofg_new_covered_lines / ns_projects.sum_existing_covered_lines %}
                        {% else %}
                             {% set ns_projects.sum_total_relative_gain = 0 %}
                        {% endif %}
                        <tr class="font-bold">
                            <td></td>
                            <td>Total</td>
                            <td>{{ accumulated_results.total_runs }}</td>
                            <td>{{ ns_projects.sum_successful_benchmarks }}</td>
                            <td>{{ ns_projects.sum_total_coverage_gain|percent }}%</td>
                            <td>{{ ns_projects.sum_total_relative_gain|percent }}%</td>
                            <td>{{ ns_projects.sum_ofg_total_covered_lines }}</td>
                            <td>{{ ns_projects.sum_ofg_new_covered_lines }}</td>
                            <td>{{ ns_projects.sum_existing_covered_lines }}</td>
                            <td>{{ ns_projects.sum_total_project_lines }}</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <div class="border rounded-lg toc-section">
        <button @click="crashesFoundOpen = !crashesFoundOpen"
                class="w-full p-4 flex justify-between items-center">
            <span class="text-lg font-bold">
                Crashes found by generated fuzz harnesses
            </span>
            {{ macros.icon_chevron_down('w-5 h-5', 'crashesFoundOpen') }}
        </button>
        <div x-show="crashesFoundOpen"
             class="p-4 border-t">

            {{ macros.expand_collapse_buttons('crashes-expand-all', 'crashes-collapse-all') }}

             <table class="sortable-table min-w-full" id="crashes-table">
                <thead>
                    <tr>
                        <th></th>
                        <th data-sorted="asc" data-sort-string>Project</th>
                        <th data-sort-number>Total crashes</th>
                        <th data-sort-number>Total benchmarks</th>
                        <th data-sort-number>True positives</th>
                        <th data-sort-number>False positives</th>
                    </tr>
                </thead>
                <tbody>
                    {% set projects_with_crashes = {} %}
                    {% for bug_sample in samples_with_bugs %}
                        {% if bug_sample.benchmark.project not in projects_with_crashes %}
                            {% set _ = projects_with_crashes.update({bug_sample.benchmark.project: {}}) %}
                        {% endif %}
                        {% if bug_sample.benchmark.id not in projects_with_crashes[bug_sample.benchmark.project] %}
                            {% set _ = projects_with_crashes[bug_sample.benchmark.project].update({bug_sample.benchmark.id: []}) %}
                        {% endif %}
                        {% set _ = projects_with_crashes[bug_sample.benchmark.project][bug_sample.benchmark.id].append(bug_sample) %}
                    {% endfor %}

                    {% set ns_projects_with_crashes = namespace(
                        sum_total_crashes=0,
                        sum_total_benchmarks=0,
                        sum_true_positives=0,
                        sum_false_positives=0) %}
                    {% for project_name, project_benchmarks in projects_with_crashes.items() %}
                    {% set project_loop = loop %}
                    {% set project_bugs = [] %}
                    {% for benchmark_bugs in project_benchmarks.values() %}
                        {% set _ = project_bugs.extend(benchmark_bugs) %}
                    {% endfor %}
                    {% set ns_projects_with_crashes.sum_total_crashes = ns_projects_with_crashes.sum_total_crashes + project_bugs|length %}
                    {% set ns_projects_with_crashes.sum_total_benchmarks = ns_projects_with_crashes.sum_total_benchmarks + project_benchmarks|length %}
                    {% set ns_projects_with_crashes.sum_true_positives = ns_projects_with_crashes.sum_true_positives + project_bugs|selectattr('sample.result.is_semantic_error', 'equalto', false)|list|length %}
                    {% set ns_projects_with_crashes.sum_false_positives = ns_projects_with_crashes.sum_false_positives + project_bugs|selectattr('sample.result.is_semantic_error', 'equalto', true)|list|length %}
                    <tr class="project-data-row">
                        <td class="table-index">
                            <button class="w-5 h-5 flex items-center justify-center focus:outline-none" @click="$refs.project_{{ loop.index }}.classList.toggle('hidden')">
                                {{ macros.icon_chevron_down('w-4 h-4', '!$refs.project_' ~ loop.index ~ '.classList.contains(\'hidden\')') }}
                            </button>
                        </td>
                        <td data-sort-value="{{ project_name }}">
                            <button @click="$refs.project_{{ loop.index }}.classList.toggle('hidden')" class="text-left font-medium hover:text-blue-600 focus:outline-none toc-subsection">
                                {{ project_name }}
                            </button>
                        </td>
                        <td data-sort-value="{{ project_bugs|length }}">{{ project_bugs|length }}</td>
                        <td data-sort-value="{{ project_benchmarks|length }}">{{ project_benchmarks|length }}</td>
                        <td data-sort-value="{{ project_bugs|selectattr('sample.result.is_semantic_error', 'equalto', false)|list|length }}">
                            {{ project_bugs|selectattr('sample.result.is_semantic_error', 'equalto', false)|list|length }}
                        </td>
                        <td data-sort-value="{{ project_bugs|selectattr('sample.result.is_semantic_error', 'equalto', true)|list|length }}">
                            {{ project_bugs|selectattr('sample.result.is_semantic_error', 'equalto', true)|list|length }}
                        </td>
                    </tr>
                    <tr>
                        <td colspan="6">
                            <div x-ref="project_{{ loop.index }}" class="hidden">
                                <div class="ml-8 mt-4">
                                    {{ macros.expand_collapse_buttons('project-expand-all-' ~ loop.index, 'project-collapse-all-' ~ loop.index, 'Expand All', 'Collapse All') }}
                                </div>
                                <div class="overflow-x-auto">
                                    <table class="sortable-table min-w-full ml-8">
                                    <thead>
                                        <tr>
                                            <th></th>
                                            <th data-sorted="asc" data-sort-string>Benchmark</th>
                                            <th data-sort-number>Crashes</th>
                                            <th data-sort-number>True positives</th>
                                            <th data-sort-number>False positives</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for benchmark_id, benchmark_bugs in project_benchmarks.items() %}
                                        <tr>
                                            <td class="table-index">
                                                <button class="w-5 h-5 flex items-center justify-center focus:outline-none" @click="$refs.samples_{{ loop.index }}_{{ project_loop.index }}.classList.toggle('hidden')">
                                                    {{ macros.icon_chevron_down('w-4 h-4', '$refs.samples_' ~ loop.index ~ '_' ~ project_loop.index ~ '.classList.contains(\'hidden\')') }}
                                                </button>
                                            </td>
                                            <td data-sort-value="{{ benchmark_id }}">
                                                <button @click="$refs.samples_{{ loop.index }}_{{ project_loop.index }}.classList.toggle('hidden')" class="text-left hover:text-blue-600 focus:outline-none toc-item">
                                                    <pre class="signature">
                                                        <a href="benchmark/{{ benchmark_id|urlencode }}/index.html" class="prettify-benchmark-name">{{ benchmark_id }}</a>
                                                    </pre>
                                                </button>
                                            </td>
                                            <td data-sort-value="{{ benchmark_bugs|length }}">{{ benchmark_bugs|length }}</td>
                                            <td data-sort-value="{{ benchmark_bugs|selectattr('sample.result.is_semantic_error', 'equalto', false)|list|length }}">
                                                {{ benchmark_bugs|selectattr('sample.result.is_semantic_error', 'equalto', false)|list|length }}
                                            </td>
                                            <td data-sort-value="{{ benchmark_bugs|selectattr('sample.result.is_semantic_error', 'equalto', true)|list|length }}">
                                                {{ benchmark_bugs|selectattr('sample.result.is_semantic_error', 'equalto', true)|list|length }}
                                            </td>
                                        </tr>
                                        <tr>
                                            <td colspan="5">
                                                <div x-ref="samples_{{ loop.index }}_{{ project_loop.index }}">
                                                    {% for bug in benchmark_bugs %}
                                                    <div class="flex items-center space-x-12 ml-56 py-1 px-4 border-l-2 border-gray-500">
                                                        <pre class="signature">
                                                            <a href="sample/{{ bug.benchmark.id|urlencode }}/{{ bug.sample.id }}.html">Sample {{ bug.sample.id }}</a>
                                                        </pre>
                                                        <span class="p-2 rounded-lg {{ 'bg-red-300 text-black' if bug.sample.result.crashes and not bug.sample.result.is_semantic_error else 'bg-green-300 text-black' }}">
                                                            {{ 'False positive' if bug.sample.result.is_semantic_error else 'True positive' }}
                                                        </span>
                                                    </div>
                                                    {% endfor %}
                                                </div>
                                            </td>
                                        </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                                </div>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                    <tr class="font-bold">
                        <td></td>
                        <td>Total</td>
                        <td>{{ ns_projects_with_crashes.sum_total_crashes }}</td>
                        <td>{{ ns_projects_with_crashes.sum_total_benchmarks }}</td>
                        <td>{{ ns_projects_with_crashes.sum_true_positives }}</td>
                        <td>{{ ns_projects_with_crashes.sum_false_positives }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>

    <div class="border rounded-lg toc-section">
        <button @click="cyclesOpen = !cyclesOpen"
                class="w-full p-4 flex justify-between items-center">
            <span class="text-lg font-bold">Cycles / Time</span>
            {{ macros.icon_chevron_down('w-5 h-5', 'cyclesOpen') }}
        </button>
        <div x-show="cyclesOpen" class="p-4 border-t space-y-8">
            <div>
                <h3 class="text-md font-semibold mb-2">Per Project</h3>
                <div class="overflow-x-auto">
                    <table class="sortable-table min-w-full">
                        <thead>
                            <tr>
                                <th data-sorted="asc">Project</th>
                                <th data-sort-number>Avg cycles/benchmark</th>
                                <th data-sort-number>Avg trial duration (min)</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% set ns_proj = namespace(total_cycles=0, total_duration=0, count=0) %}
                            {% for project_name, pdata in unified_data.items() %}
                            {% set cycles = (pdata.get('average_cycles_per_benchmark') or 0) | float %}
                            {% set duration = (pdata.get('average_trial_duration_sec') or 0) | float %}
                            {% set ns_proj.total_cycles = ns_proj.total_cycles + cycles %}
                            {% set ns_proj.total_duration = ns_proj.total_duration + duration %}
                            {% set ns_proj.count = ns_proj.count + 1 %}
                            <tr>
                                <td data-sort-value="{{ project_name }}">{{ project_name }}</td>
                                <td data-sort-value="{{ cycles }}">{{ cycles }}</td>
                                <td data-sort-value="{{ (duration / 60) | round(2) }}">{{ (duration / 60) | round(2) }}</td>
                            </tr>
                            {% endfor %}
                            {% if ns_proj.count > 0 %}
                            <tr class="font-bold">
                                <td>Average</td>
                                <td>{{ (ns_proj.total_cycles / ns_proj.count) | round(2) }}</td>
                                <td>{{ ((ns_proj.total_duration / ns_proj.count) / 60) | round(2) }}</td>
                            </tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div>
            </div>

            <div>
                <h3 class="text-md font-semibold mb-2">Per Benchmark</h3>
                <div class="overflow-x-auto">
                    <table class="sortable-table min-w-full">
                        <thead>
                            <tr>
                                <th>Project</th>
                                <th data-sorted="asc">Benchmark</th>
                                <th data-sort-number>Average cycles/sample</th>
                                <th data-sort-number>Average trial duration (min)</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% set ns_bench = namespace(total_cycles=0, total_duration=0, count=0) %}
                            {% for project_name, pdata in unified_data.items() %}
                                {% if pdata.benchmarks %}
                                    {% for bench_id, bdata in pdata.benchmarks.items() %}
                                    {% set cycles = (bdata.get('avg_cycles_per_sample') or 0) | float %}
                                    {% set duration = (bdata.get('avg_trial_duration_sec') or 0) | float %}
                                    {% set ns_bench.total_cycles = ns_bench.total_cycles + cycles %}
                                    {% set ns_bench.total_duration = ns_bench.total_duration + duration %}
                                    {% set ns_bench.count = ns_bench.count + 1 %}
                                    <tr>
                                        <td data-sort-value="{{ project_name }}">{{ project_name }}</td>
                                        <td data-sort-value="{{ bench_id }}">
                                            <pre class="signature">
                                                <a href="benchmark/{{ bench_id|urlencode }}/index.html" class="prettify-benchmark-name">{{ bench_id }}</a>
                                            </pre>
                                        </td>
                                        <td data-sort-value="{{ cycles }}">{{ cycles }}</td>
                                        <td data-sort-value="{{ (duration / 60) | round(2) }}">{{ (duration / 60) | round(2) }}</td>
                                    </tr>
                                    {% endfor %}
                                {% endif %}
                            {% endfor %}
                            {% if ns_bench.count > 0 %}
                            <tr class="font-bold">
                                <td>Average</td>
                                <td>-</td>
                                <td>{{ (ns_bench.total_cycles / ns_bench.count) | round(2) }}</td>
                                <td>{{ ((ns_bench.total_duration / ns_bench.count) / 60) | round(2) }}</td>
                            </tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div>
            </div>

            <div>
                <h3 class="text-md font-semibold mb-2">Per Sample</h3>
                <div class="overflow-x-auto">
                    <table class="sortable-table min-w-full">
                        <thead>
                            <tr>
                                <th>Project</th>
                                <th>Benchmark</th>
                                <th data-sorted="asc">Sample</th>
                                <th data-sort-number>Cycles</th>
                                <th data-sort-number>Trial duration (min)</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% set ns_sample = namespace(total_cycles=0, total_duration=0, count=0) %}
                            {% for project_name, pdata in unified_data.items() %}
                                {% if pdata.benchmarks %}
                                    {% for bench_id, bdata in pdata.benchmarks.items() %}
                                        {% if bdata.samples %}
                                            {% for s in bdata.samples %}
                                            {% set cycles = (s.get('cycles_count') or 0) | int %}
                                            {% set duration = (s.get('trial_avg_duration_sec') or 0) | float %}
                                            {% set ns_sample.total_cycles = ns_sample.total_cycles + cycles %}
                                            {% set ns_sample.total_duration = ns_sample.total_duration + duration %}
                                            {% set ns_sample.count = ns_sample.count + 1 %}
                                            <tr>
                                                <td data-sort-value="{{ project_name }}">{{ project_name }}</td>
                                                <td data-sort-value="{{ bench_id }}">
                                                    <pre class="signature">
                                                        <a href="benchmark/{{ bench_id|urlencode }}/index.html" class="prettify-benchmark-name">{{ bench_id }}</a>
                                                    </pre>
                                                </td>
                                                <td data-sort-value="{{ s.sample }}">
                                                    <pre class="signature">
                                                        <a href="sample/{{ bench_id|urlencode }}/{{ s.sample }}.html">Sample {{ s.sample }}</a>
                                                    </pre>
                                                </td>
                                                <td data-sort-value="{{ cycles }}">{{ cycles }}</td>
                                                <td data-sort-value="{{ (duration / 60) | round(2) }}">{{ (duration / 60) | round(2) }}</td>
                                            </tr>
                                            {% endfor %}
                                        {% endif %}
                                    {% endfor %}
                                {% endif %}
                            {% endfor %}
                            {% if ns_sample.count > 0 %}
                            <tr class="font-bold">
                                <td>Average</td>
                                <td>-</td>
                                <td>-</td>
                                <td>{{ (ns_sample.total_cycles / ns_sample.count) | round(2) }}</td>
                                <td>{{ ((ns_sample.total_duration / ns_sample.count) / 60) | round(2) }}</td>
                            </tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>

    <div class="border rounded-lg toc-section">
        <button @click="languageOpen = !languageOpen"
                class="w-full p-4 flex justify-between items-center">
            <span class="text-lg font-bold">
                Language coverage
            </span>
            {{ macros.icon_chevron_down('w-5 h-5', 'languageOpen') }}
        </button>

        <div x-show="languageOpen"
             class="p-4 border-t">
             <table class="sortable-table mb-8" id="language-coverage-gain">
                <thead>
                    <th data-sorted="asc" data-sort-string>Language</th>
                    <th data-sort-number>OSS-Fuzz total lines</th>
                    <th data-sort-number>OSS-Fuzz coverage lines</th>
                    <th data-sort-number>Experiment new coverage lines</th>
                    <th data-sort-number>Increase of total</th>
                    <th data-sort-number>Increase of covered</th>
                </thead>
                <tbody>
                    {% for language in coverage_language_gains['coverage_gains_per_language'] %}
                    <tr>
                        <td data-sort-value="{{language}}">{{language}}</td>
                        <td data-sort-value="{{ coverage_language_gains['oss_fuzz_language_status'].get(language, {}).get('total', 'N/A') }}">{{ coverage_language_gains['oss_fuzz_language_status'].get(language, {}).get('total', 'N/A') }}</td>
                        <td data-sort-value="{{ coverage_language_gains['oss_fuzz_language_status'].get(language, {}).get('covered', 'N/A') }}">{{ coverage_language_gains['oss_fuzz_language_status'].get(language, {}).get('covered', 'N/A') }}</td>
                        <td data-sort-value="{{ coverage_language_gains['coverage_gains_per_language'].get(language, 'N/A') }}">{{ coverage_language_gains['coverage_gains_per_language'].get(language, 'N/A') }}</td>
                        <td data-sort-value="{{ coverage_language_gains['comperative_coverage_gains'].get(language, {}).get('total_coverage_increase', 'N/A') }}">{{ coverage_language_gains['comperative_coverage_gains'].get(language, {}).get('total_coverage_increase', 'N/A') }}%</td>
                        <td data-sort-value="{{ coverage_language_gains['comperative_coverage_gains'].get(language, {}).get('relative_coverage_increase', 'N/A') }}">{{ coverage_language_gains['comperative_coverage_gains'].get(language, {}).get('relative_coverage_increase', 'N/A') }}%</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <div class="container mx-auto mt-8">
        <div class="border rounded-lg toc-section">
            <button @click="chartsOpen = !chartsOpen"
                    class="w-full p-4 flex justify-between items-center">
                <span class="text-lg font-bold">
                    Charts
                </span>
                {{ macros.icon_chevron_down('w-5 h-5', 'chartsOpen') }}
            </button>
            <div x-show="chartsOpen" class="p-4 border-t">
                <div class="chart-row">
                    <div id="coverage-chart" class="chart-box" style="grid-column: 1 / -1; width: 100%"></div>
                    <div id="language-coverage-chart" class="chart-box" style="display:none"></div>
                </div>
                <div class="chart-row">
                    <div id="crash-reasons-chart" class="chart-box"></div>
                </div>
                <div class="chart-row">
                    <div id="cycles-projects-chart" class="chart-box"></div>
                    <div id="duration-projects-chart" class="chart-box"></div>
                </div>
                <div class="chart-row">
                    <div id="cycles-benchmarks-chart" class="chart-box"></div>
                    <div id="duration-benchmarks-chart" class="chart-box"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<button class="toc-toggle" id="tocToggle">
    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">Add commentMore actions
        <line x1="3" y1="12" x2="21" y2="12"></line>
        <line x1="3" y1="6" x2="21" y2="6"></line>
        <line x1="3" y1="18" x2="21" y2="18"></line>
    </svg>
</button>


<div id="toc" class="toc">
    <div class="toc-header">
        <h3 class="text-lg font-semibold">Table of Contents</h3>
    </div>
    <ul class="toc-tree" id="toc-tree"></ul>
</div>

{% endblock %}
